package thread;

import depot.Customer;
import mvc.ObserverManager;

public class WorkThread implements Runnable{
	
	private ObserverManager oManager;
	private int seqNo; 
	private String worker_name; 
	
	/**
	 * this is the constructor of WorkThread, and it includes all related variables
	 * @param n  it stands for possible happened situation
	 * @param oM it includes all the demanded methods
	 */
	public WorkThread(int n, ObserverManager oM){
		this.oManager = oM;
		
		//this shows possible conditions
		switch(n)
		{
		case 0: worker_name = "Ximeng Bao";
			break;
		case 1: worker_name = "Jingying Niu";
			break;
		}
	}
	
	/**
	 * this method is to deal with parcel collection by two workers, and 
	 * the two workers are two threads. They cope with parcels by turn.
	 */
	
	public void run() {
		while(oManager.getState()){
			
			if(!oManager.getCustomerList().isEmpty()){
				try {
					Thread.sleep(oManager.getSpeed());
					} 
				
				catch (InterruptedException e)
				{
					e.printStackTrace();
					}
			
				try{
					this.seqNo = oManager.getSeqNo();
					Customer cm = oManager.getAllcustomer().findCustomer(seqNo);
					
					if(cm != null)
					{
						oManager.process(cm, String.valueOf(worker_name));
					
						oManager.removeCustomer(cm.getParcelID());
						oManager.setSeqNo(seqNo+1);
					}	
				}
				catch (Exception e) {
					e.getMessage();
				}
			}
			
		}
	}
	
}
